//
//  MLViewController.m
//  07_23_HomeWork
//
//  Created by GivenBmli on 16/7/25.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "MLViewController.h"
#import "ML_First.h"
#import "ML_Second.h"
#import "ML_Third.h"
#import "ML_Four.h"
#import "ML_Five.h"
#define WIDETH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#define COLORAUTO [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0]


#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
@interface MLViewController ()
@property (nonatomic, strong) UIView *viewTabBar;
@property (nonatomic, strong) UIButton *btn;
@property (nonatomic, assign) BOOL isPanduan;
@property (nonatomic, strong) NSMutableArray *arrBtn;
@property (nonatomic, assign) NSInteger n;
@property (nonatomic, strong) UIViewController *showVC;
@end

@implementation MLViewController
- (NSMutableArray *)arrBtn {
    if (!_arrBtn) {
        self.arrBtn = [NSMutableArray array];
    }
    return _arrBtn;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"导航栏";
    self.viewTabBar = [[UIView alloc] initWithFrame:CGRectMake(0, kHeight - 49, WIDETH, 49)];
    _viewTabBar.backgroundColor = [UIColor redColor];
    [self.view addSubview:_viewTabBar];
    [self creatChildView];
    [self creatBtn];
}
- (void)creatChildView {
    ML_First *first = [[ML_First alloc] init];
    [self addChildViewController:first];
    ML_Second *second = [[ML_Second alloc] init];
    [self addChildViewController:second];
    ML_Third *third = [[ML_Third alloc] init];
    [self addChildViewController:third];
    ML_Four *four = [[ML_Four alloc] init];
    [self addChildViewController:four];
    ML_Five *five = [[ML_Five alloc] init];
    [self addChildViewController:five];
}
- (void)creatBtn {
    NSArray *arr = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
    for (int i = 0; i < arr.count ; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(kWidth / 5 * i, 0, kWidth / 5, 49)];
        [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tu%d", i + 1]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tu%d", i + 1]] forState:UIControlStateNormal];
        btn.tag = 1001 + i;
        
        btn.backgroundColor = [UIColor colorWithWhite:0.702 alpha:1.000];
        [btn addTarget:self action:@selector(addBtnStateBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.arrBtn addObject:btn];
        [self.viewTabBar addSubview:btn];
        if (i == 0) {
            [self addBtnStateBtn:btn];
        }
    }
}
- (void)addBtnStateBtn:(UIButton *)btn {
    [self creabtn:btn];
    NSInteger i = btn.tag - 1001;
    [self setUpChild:i];
}
- (void)creabtn:(UIButton *)btn {
    NSInteger i = btn.tag - 1000;
    [_btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tu%ld", _n]]  forState:UIControlStateNormal];
    [UIView animateWithDuration:0.1 animations:^{
        btn.imageView.transform = CGAffineTransformMakeScale(0.8, 0.8);
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            btn.imageView.transform = CGAffineTransformMakeScale(1, 1);
        }completion:^(BOOL finished) {
        }];
    }];
    [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%ld", i]] forState:UIControlStateNormal];
    self.btn = btn;
    self.n = i;
}
- (void)setUpChild:(NSInteger)i {
    [_showVC.view removeFromSuperview];
    self.showVC = self.childViewControllers[i];
    if (_showVC.view.superview) {
        return;
    }
    _showVC.view.frame = CGRectMake(0, 0, kWidth, kHeight - 49);
    [self.view addSubview:_showVC.view];
}

@end
